# 导包
from selenium import webdriver
from selenium.webdriver.common.by import By
# 将页面元素操作封装到一个类
class PageLogin:
    def __init__(self):  # 初始化（构造方法）
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get('http://192.168.88.100:8000/admin/')
    # 输入用户名
    def page_input_username(self, username):
        el = self.driver.find_element(By.CSS_SELECTOR, '#id_username')
        el.clear() # 把之前的清空
        el.send_keys(username)
    def page_input_password(self, password): # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, '#id_password').send_keys(password)
    def page_click_login_btn(self): # 点击登录
        self.driver.find_element(By.CSS_SELECTOR, '[type="submit"]').click()
    def page_login(self, username, password): # 组装登录功能
        self.page_input_username(username)
        self.page_input_password(password)
        self.page_click_login_btn()
    def page_get_text(self): # 获取结果
        return self.driver.find_element(By.CSS_SELECTOR, '.errornote').text
    def page_close(self):  # 销毁
        self.driver.quit()

if __name__ == '__main__':
    # 测试
    login = PageLogin()
    login.page_login('admin', '888999')
    result = login.page_get_text()
    print('结果：', result)
    login.page_close()